#include <sw/redis++/redis++.h>
using namespace sw::redis;
#include <iostream>
#include <string>

void RetryConnection(Redis &redis)
{
    std::string s1 = redis.ping();
    int cnt = 5;
    while (s1 != "PONG" && cnt)
    {
        --cnt;
        s1 = redis.ping();
    }
}
void unity1(Redis &redis)
{
    printf("开始测试sadd和smembers\n");
    redis.flushall();
    long long ret = redis.sadd("myset", "helloset");
    ret += redis.sadd("myset", "2");
    ret += redis.sadd("myset", "3");
    ret += redis.sadd("myset", "4");
    std::cout << "sadd执行结果: " << ret << std::endl;
    // 查询
    std::vector<std::string> res;
    auto it = std::back_inserter(res);
    redis.smembers("myset", it);
    for (auto &s : res)
    {
        std::cout << s << std::endl;
    }
    std::cout << "---------------------" << std::endl;
}
const StringView key = "myset";
void unity2(Redis &redis)
{
    printf("开始测试sismember和scard了\n");
    redis.flushall();
    long long ret = redis.sadd("myset", "1");
    ret += redis.sadd("myset", "2");
    ret += redis.sadd("myset", "3");
    ret += redis.sadd("myset", "4");
    bool flag = redis.sismember(key, "1");
    std::cout << "sismember执行结果: " << flag << std::endl;
    flag = redis.sismember(key, "2");
    std::cout << "sismember执行结果: " << flag << std::endl;
    flag = redis.sismember(key, "3");
    std::cout << "sismember执行结果: " << flag << std::endl;
    flag = redis.sismember(key, "4");
    std::cout << "sismember执行结果: " << flag << std::endl;
    flag = redis.sismember(key, "10");
    std::cout << "sismember执行结果: " << flag << std::endl;

    std::cout << "---------------------------" << std::endl;

    std::cout << "set的元素个数: " << redis.scard(key) << std::endl;
}

void unity3(Redis &redis)
{
    {
        printf("开始测试spop和smove,Srem了\n");
        redis.flushall();
        long long ret = redis.sadd(key, "1");
        std::cout << ret << std::endl;
        ret += redis.sadd("myset", "2");
        ret += redis.sadd("myset", "3");
        ret += redis.sadd("myset", "40");
        OptionalString opstr = redis.spop(key);
        if (opstr.has_value())
        {
            std::cout << "opstr: " << opstr.value() << std::endl;
        }
        // 构造第二个集合
        long long ret2 = redis.sadd("myset2", "1");
        ret2 += redis.sadd("myset2", "2");
        ret2 += redis.sadd("myset2", "3");
        ret2 += redis.sadd("myset2", "4");
        std::cout << "ret2: " << ret2 << std::endl;
        bool flag = redis.smove(key, "myset2", "40");
        std::cout << "flag: " << flag << std::endl;

        std::cout << "--------------------------" << std::endl;
        std::vector<std::string> res;
        auto it = std::back_inserter(res);
        redis.smembers(key, it);
        for (auto &s : res)
        {
            std::cout << s << std::endl;
        }
        std::cout << std::endl;
        long long ret3 = redis.srem(key, "1");
        std::cout << "ret3: " << ret3 << std::endl;
        ret3 += redis.srem(key, "2");
        std::cout << "ret3: " << ret3 << std::endl;
        ret3 += redis.srem(key, "3");
        std::cout << "ret3: " << ret3 << std::endl;
        ret3 += redis.srem(key, "40");
        std::cout << "ret3: " << ret3 << std::endl;
    }
}
int main()
{
    Redis redis("tcp://127.0.0.1:6379");
    RetryConnection(redis);
    return 0;
}